home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / src / listmode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  8.0 KB  |  329 lines

  1. /* Directory panel listing format editor -- for the Midnight Commander
  2.    Copyright (C) 1994, 1995 The Free Software Foundation
  3.  
  4.    Written by: 1994 Radek Doulik
  5.               1995 Janne Kukonlehto
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  20.  */
  21.  
  22. #include <config.h>
  23. #ifdef HAVE_UNISTD_H
  24. #   include <unistd.h>
  25. #endif
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>        /* For malloc() */
  29. #include <sys/types.h>
  30. #include <sys/param.h>
  31. #include <sys/stat.h>
  32. #include <grp.h>
  33. #include <pwd.h>
  34. #include "tty.h"
  35. #include "mad.h"
  36. #include "util.h"        /* Needed for the externs */
  37. #include "win.h"
  38. #include "color.h"
  39. #include "dlg.h"
  40. #include "widget.h"
  41. #include "dialog.h"        /* For do_refresh() */
  42. #include "wtools.h"
  43.  
  44. /* Needed for the extern declarations of integer parameters */
  45. #include "dir.h"
  46. #include "panel.h"        /* Needed for the externs */
  47. #include "file.h"
  48. #include "main.h"
  49. #include "global.h"
  50. #include "listmode.h"
  51.  
  52. #define UX        5
  53. #define UY        2
  54.  
  55. #define BX        5
  56. #define BY        18
  57.  
  58. #define BUTTONS        4
  59. #define LABELS          4
  60. #define B_ADD        B_USER
  61. #define B_REMOVE        B_USER + 1
  62.  
  63. static WListbox *l_listmode;
  64.  
  65. static Dlg_head *listmode_dlg;
  66.  
  67. static WLabel *pname;
  68.  
  69. static char *listmode_section = "[Listing format edit]";
  70.  
  71. static char *s_genwidth [2] = {"Half width", "Full width"};
  72. WRadio *radio_genwidth;
  73. static char *s_columns [2] = {"One column", "Two columns"};
  74. WRadio *radio_columns;
  75. static char *s_justify [3] =
  76. {"Left justified", "Default justification", "Right justified"};
  77. WRadio *radio_justify;
  78. static char *s_itemwidth [3] =
  79. {"Free width", "Fixed width", "Growable width"};
  80. WRadio *radio_itemwidth;
  81.  
  82. struct {
  83.     int ret_cmd, y, x;
  84.     char *text;
  85.     int hkey, hpos;
  86. } listmode_but[BUTTONS] = {
  87.     { B_CANCEL, 0, 53, "[ Cancel ]",  'c', 2, },
  88.     { B_ADD,    0, 22, "[ Add item ]", 'a', 2, },
  89.     { B_REMOVE, 0, 10, "[ Remove ]",  'r', 2, },
  90.     { B_ENTER,  0,  0, "[[ Ok ]]", 'o', 3, },
  91. };
  92.  
  93. #define B_PLUS B_USER
  94. #define B_MINUS B_USER+1
  95.  
  96. struct {
  97.     int y, x;
  98.     char *text;
  99. } listmode_text [LABELS] = {
  100.     { UY, UX + 1, " General options " },
  101.     { UY+4, UX+1, " Items "},
  102.     { UY+4, UX+21, " Item options" },
  103.     { UY+13, UX+22, "Item width:" }
  104. };
  105.  
  106. #ifndef HAVE_X
  107. static void listmode_refresh (void)
  108. {
  109.     attrset (REVERSE_COLOR);
  110.     dlg_erase (listmode_dlg);
  111.     
  112.     draw_box (listmode_dlg, 1, 2, 20, 70);
  113.     draw_box (listmode_dlg, UY, UX, 4, 63);
  114.     draw_box (listmode_dlg, UY + 4, UX, 11, 18);
  115.     draw_box (listmode_dlg, UY + 4, UX+20, 11, 43);
  116. }
  117. #endif
  118.  
  119. static int bplus_cback (int action, void *data)
  120. {
  121.     return 0;
  122. }
  123.  
  124. static int bminus_cback (int action, void *data)
  125. {
  126.     return 0;
  127. }
  128.  
  129. static int listmode_callback (Dlg_head * h, int Par, int Msg)
  130. {
  131.     switch (Msg) {
  132. #ifndef HAVE_X    
  133.     case DLG_DRAW:
  134.     listmode_refresh ();
  135.     break;
  136. #endif    
  137.  
  138.     case DLG_POST_KEY:
  139.     /* fall */
  140.     case DLG_INIT:
  141.     attrset (REVERSE_COLOR);
  142.     dlg_move (h, UY+13, UX+35);
  143.     printw ("%02d", 99);
  144.     attrset (MENU_ENTRY_COLOR);
  145.     break;
  146.     }
  147.     return 0;
  148. }
  149.  
  150. static int l_call (void *data)
  151. {
  152.     return 1;
  153. }
  154.  
  155. static void init_listmode (char *oldlistformat)
  156. {
  157.     int i;
  158.     char *s;
  159.     int format_width = 0;
  160.     int format_columns = 0;
  161.  
  162.     do_refresh ();
  163.  
  164.     listmode_dlg = create_dlg (0, 0, 22, 74, dialog_colors,
  165.                   listmode_callback, listmode_section, "listmode",
  166.                   DLG_CENTER);
  167.     x_set_dialog_title (listmode_dlg, "Listing format edit");        
  168.  
  169. #define XTRACT(i) BY+listmode_but[i].y, BX+listmode_but[i].x, listmode_but[i].ret_cmd, listmode_but[i].text, listmode_but[i].hkey, listmode_but[i].hpos, 0, 0
  170.  
  171.     for (i = 0; i < BUTTONS; i++)
  172.     add_widgetl (listmode_dlg, button_new (XTRACT (i)), (i == BUTTONS - 1) ?
  173.         XV_WLAY_CENTERROW : XV_WLAY_RIGHTOF);
  174.  
  175.     /* We add the labels. */
  176.     for (i = 0; i < LABELS; i++){
  177.     pname = label_new (listmode_text [i].y,
  178.                listmode_text [i].x, listmode_text [i].text);
  179.     add_widget (listmode_dlg, pname);
  180.     }
  181.  
  182.     add_widget (listmode_dlg, button_new (UY+13, UX+37, B_MINUS,
  183.                       "-", '-', 0, bminus_cback, 0));
  184.     add_widget (listmode_dlg, button_new (UY+13, UX+34, B_PLUS,
  185.                       "+", '+', 0, bplus_cback, 0));
  186.     radio_itemwidth = radio_new (UY+9, UX+22, 3, s_itemwidth, 1);
  187.     add_widget (listmode_dlg, radio_itemwidth);
  188.     radio_itemwidth = 0;
  189.     radio_justify = radio_new (UY+5, UX+22, 3, s_justify, 1);
  190.     add_widget (listmode_dlg, radio_justify);
  191.     radio_justify->sel = 1;
  192.  
  193.     /* get new listbox */
  194.     l_listmode = listbox_new (UY + 5, UX + 1, 16, 9, 0, l_call);
  195.  
  196.     if (strncmp (oldlistformat, "full ", 5) == 0){
  197.     format_width = 1;
  198.     oldlistformat += 5;
  199.     }
  200.     if (strncmp (oldlistformat, "half ", 5) == 0){
  201.     oldlistformat += 5;
  202.     }
  203.     if (strncmp (oldlistformat, "2 ", 2) == 0){
  204.     format_columns = 1;
  205.     oldlistformat += 2;
  206.     }
  207.     if (strncmp (oldlistformat, "1 ", 2) == 0){
  208.     oldlistformat += 2;
  209.     }
  210.     s = strtok (oldlistformat, ",");
  211.  
  212.     while (s){
  213.     listbox_add_item (l_listmode, 0, 0, s, NULL);
  214.     s = strtok (NULL, ",");
  215.     }
  216.  
  217.     /* add listbox to the dialogs */
  218.     add_widgetl (listmode_dlg, l_listmode, XV_WLAY_EXTENDWIDTH);
  219.  
  220.     radio_columns = radio_new (UY+1, UX+32, 2, s_columns, 1);
  221.     add_widget (listmode_dlg, radio_columns);
  222.     radio_columns->sel = format_columns;
  223.     radio_genwidth = radio_new (UY+1, UX+2, 2, s_genwidth, 1);
  224.     add_widget (listmode_dlg, radio_genwidth);
  225.     radio_genwidth->sel = format_width;
  226. }
  227.  
  228. static void listmode_done (void)
  229. {
  230.     destroy_dlg (listmode_dlg);
  231.     if (0)
  232.     update_panels (UP_OPTIMIZE, UP_KEEPSEL, UP_KEEPSEL);
  233.     repaint_screen ();
  234. }
  235.  
  236. char *select_new_item (void)
  237. {
  238.     /* NOTE: The following array of possible items must match the
  239.        formats array in screen.c. Better approach might be to make the
  240.        formats array global */
  241.     char *possible_items [] =
  242.     { "name", "size", "type", "mtime", "perm", "mode", "|", "nlink",
  243.       "owner", "group", "atime", "ctime", "space", "mark",
  244.       "inode", NULL };
  245.  
  246.     int i;
  247.     Listbox *mylistbox;
  248.  
  249.     mylistbox = create_listbox_window (12, 20, " Add listing format item ", listmode_section);
  250.     for (i = 0; possible_items [i]; i++){
  251.     listbox_add_item (mylistbox->list, 0, 0, possible_items [i], NULL);
  252.     }
  253.  
  254.     i = run_listbox (mylistbox);
  255.     if (i >= 0)
  256.     return possible_items [i];
  257.     else
  258.     return NULL;
  259. }
  260.  
  261. char *collect_new_format (void)
  262. {
  263.     char *newformat;
  264.     int i;
  265.     char *last;
  266.     char *text, *extra;
  267.  
  268.     newformat = xmalloc (1024, "collect_new_format");
  269.     if (radio_genwidth->sel)
  270.     strcpy (newformat, "full ");
  271.     else
  272.     strcpy (newformat, "half ");
  273.     if (radio_columns->sel)
  274.     strcat (newformat, "2 ");
  275.     last = NULL;
  276.     for (i = 0;;i++){
  277.     listbox_select_by_number (l_listmode, i);
  278.     listbox_get_current (l_listmode, &text, &extra);
  279.     if (text == last)
  280.         break;
  281.     if (last != NULL)
  282.         strcat (newformat, ",");
  283.     strcat (newformat, text);
  284.     last = text;
  285.     }
  286.     return newformat;
  287. }
  288.  
  289. char *listmode_edit (char *oldlistformat)
  290. {
  291.     char *newformat = NULL;
  292.     char *s;
  293.  
  294.     s = strdup (oldlistformat);
  295.     init_listmode (s);
  296.     free (s);
  297.  
  298.     while (newformat == NULL)
  299.     {
  300.     /* display file info */
  301.     attrset (SELECTED_COLOR);
  302.  
  303.     run_dlg (listmode_dlg);
  304.  
  305.     switch (listmode_dlg->ret_value) {
  306.     case B_CANCEL:
  307.         newformat = strdup (oldlistformat);
  308.         break;
  309.  
  310.     case B_ADD:
  311.         s = select_new_item ();
  312.         if (s)
  313.         listbox_add_item (l_listmode, 0, 0, s, NULL);
  314.         break;
  315.  
  316.     case B_REMOVE:
  317.         listbox_remove_current (l_listmode);
  318.         break;
  319.  
  320.     case B_ENTER:
  321.         newformat = collect_new_format ();
  322.         break;
  323.     }
  324.     }
  325.  
  326.     listmode_done ();
  327.     return newformat;
  328. }
  329.